﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{

    public class ZingTop100
    {
        private string _LocalLink;
        private string _LinkTop100NhacTre;
        private string _LinkTop100NhacTruTinh;
        private string _LinkTop100NhacCachMang;
        private string _LinkTop100NhacRockViet;
        private string _LinkTop100NhacRapViet;
        private string _LinkTop100NhacThieuNhi;
        private string _LinkTop100NhacTrinh;
        /// top 100 nhac quoc te
        private string _LinkTop100Pop;
        private string _LinkTop100Rock;
        private string _LinkTop100Rap;
        private string _LinkTop100Country;

        private bool _IsSetTop100NhacTre;
        private bool _IsSetTop100NhacTruTinh;
        private bool _IsSetTop100NhacCachMang;
        private bool _IsSetTop100NhacRockViet;
        private bool _IsSetTop100NhacRapViet;
        private bool _IsSetTop100NhacThieuNhi;
        private bool _IsSetTop100NhacTrinh;
        /// top 100 nhac quoc te
        private bool _IsSetTop100Pop;
        private bool _IsSetTop100Rock;
        private bool _IsSetTop100Rap;
        private bool _IsSetTop100Country;


        #region Content
        ///  top 100 nhac viet nam
        private ZingTop100Object _Top100NhacTre;

        public ZingTop100Object Top100NhacTre
        {
            get { return _Top100NhacTre; }
            private set { _Top100NhacTre = value; }
        }

        private ZingTop100Object _Top100NhacTruTinh;

        public ZingTop100Object Top100NhacTruTinh
        {
            get { return _Top100NhacTruTinh; }
            private set { _Top100NhacTruTinh = value; }
        }

        private ZingTop100Object _Top100NhacCachMang;

        public ZingTop100Object Top100NhacCachMang
        {
            get { return _Top100NhacCachMang; }
            private set { _Top100NhacCachMang = value; }
        }

        private ZingTop100Object _Top100NhacRockViet;

        public ZingTop100Object Top100NhacRockViet
        {
            get { return _Top100NhacRockViet; }
            private set { _Top100NhacRockViet = value; }
        }

        private ZingTop100Object _Top100NhacRapViet;

        public ZingTop100Object Top100NhacRapViet
        {
            get { return _Top100NhacRapViet; }
            private set { _Top100NhacRapViet = value; }
        }

        private ZingTop100Object _Top100NhacThieuNhi;

        public ZingTop100Object Top100NhacThieuNhi
        {
            get { return _Top100NhacThieuNhi; }
            private set { _Top100NhacThieuNhi = value; }
        }

        private ZingTop100Object _Top100NhacTrinh;

        public ZingTop100Object Top100NhacTrinh
        {
            get { return _Top100NhacTrinh; }
            private set { _Top100NhacTrinh = value; }
        }

        /// top 100 nhac quoc te
        private ZingTop100Object _Top100Pop;

        public ZingTop100Object Top100Pop
        {
            get { return _Top100Pop; }
            private set { _Top100Pop = value; }
        }

        private ZingTop100Object _Top100Rock;

        public ZingTop100Object Top100Rock
        {
            get { return _Top100Rock; }
            private set { _Top100Rock = value; }
        }

        private ZingTop100Object _Top100Rap;

        public ZingTop100Object Top100Rap
        {
            get { return _Top100Rap; }
            private set { _Top100Rap = value; }
        }

        private ZingTop100Object _Top100Country;

        public ZingTop100Object Top100Country
        {
            get { return _Top100Country; }
            private set { _Top100Country = value; }
        }

        #endregion
        public ZingTop100(string url)
        {
            _LocalLink = url;
            _LinkTop100NhacTre = "";
            _LinkTop100NhacTruTinh = "";
            _LinkTop100NhacCachMang = "";
            _LinkTop100NhacRockViet = "";
            _LinkTop100NhacRapViet = "";
            _LinkTop100NhacThieuNhi = "";
            _LinkTop100NhacTrinh = "";
            /// top 100 nhac quoc te
            _LinkTop100Pop = "";
            _LinkTop100Rock = "";
            _LinkTop100Rap = "";
            _LinkTop100Country = "";

            _IsSetTop100NhacTre = false;
            _IsSetTop100NhacTruTinh = false;
            _IsSetTop100NhacCachMang = false;
            _IsSetTop100NhacRockViet = false;
            _IsSetTop100NhacRapViet = false;
            _IsSetTop100NhacThieuNhi = false;
            _IsSetTop100NhacTrinh = false;
            /// top 100 nhac quoc te
            _IsSetTop100Pop = false;
            _IsSetTop100Rock = false;
            _IsSetTop100Rap = false;
            _IsSetTop100Country = false;

        }

        private async Task GetLink()
        {
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LocalLink));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34 || source[i] == '\n')
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }
                string headLink = @"http://m.mp3.zing.vn/";
                string pattern = @"(?=top-100/bai-hat-Nhac-Tre).*?(html)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                _LinkTop100NhacTre = headLink + mc[0].Value;


                pattern = @"(?=top-100/bai-hat-Nhac-Tru-Tinh).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacTruTinh = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Rap-Viet).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacRapViet = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Rock-Viet).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacRockViet = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Nhac-Cach-Mang).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacCachMang = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Nhac-Trinh).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacTrinh = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Nhac-Thieu-Nhi).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100NhacThieuNhi = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Pop).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100Pop = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Rock).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100Rock = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Rap-Hiphop).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100Rap = headLink + mc[0].Value;

                pattern = @"(?=top-100/bai-hat-Country).*?(html)";
                regex = new Regex(pattern);
                mc = regex.Matches(source);
                _LinkTop100Country = headLink + mc[0].Value;
                return;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacTre()
        {
            if (_IsSetTop100NhacTre)
                return;
            try 
            {
                if (_LinkTop100NhacTre == "")
                    await GetLink();
                _Top100NhacTre = new ZingTop100Object(_LinkTop100NhacTre);
                await _Top100NhacTre.SetAllInformation();
                _IsSetTop100NhacTre = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacTruTinh()
        {
            if (_IsSetTop100NhacTruTinh)
                return;
            try
            {
                if (_LinkTop100NhacTruTinh == "")
                    await GetLink();
                _Top100NhacTruTinh = new ZingTop100Object(_LinkTop100NhacTruTinh);
                await _Top100NhacTruTinh.SetAllInformation();
                _IsSetTop100NhacTruTinh = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacTrinh()
        {
            if (_IsSetTop100NhacTrinh)
                return;
            try
            {
                if (_LinkTop100NhacTrinh == "")
                    await GetLink();
                _Top100NhacTrinh = new ZingTop100Object(_LinkTop100NhacTrinh);
                await _Top100NhacTrinh.SetAllInformation();
                _IsSetTop100NhacTrinh = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacThieuNhi()
        {
            if (_IsSetTop100NhacThieuNhi)
                return;
            try
            {
                if (_LinkTop100NhacThieuNhi == "")
                    await GetLink();
                _Top100NhacThieuNhi = new ZingTop100Object(_LinkTop100NhacThieuNhi);
                await _Top100NhacThieuNhi.SetAllInformation();
                _IsSetTop100NhacThieuNhi = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacRapViet()
        {
            if (_IsSetTop100NhacRapViet)
                return;
            try
            {
                if (_LinkTop100NhacRapViet == "")
                    await GetLink();
                _Top100NhacRapViet = new ZingTop100Object(_LinkTop100NhacRapViet);
                await _Top100NhacRapViet.SetAllInformation();
                _IsSetTop100NhacRapViet = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacRockViet()
        {
            if (_IsSetTop100NhacRockViet)
                return;
            try
            {
                if (_LinkTop100NhacRockViet == "")
                    await GetLink();
                _Top100NhacRockViet = new ZingTop100Object(_LinkTop100NhacRockViet);
                await _Top100NhacRockViet.SetAllInformation();
                _IsSetTop100NhacRockViet = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100NhacCachMang()
        {
            if (_IsSetTop100NhacCachMang)
                return;
            try
            {
                if (_LinkTop100NhacCachMang == "")
                    await GetLink();
                _Top100NhacCachMang = new ZingTop100Object(_LinkTop100NhacCachMang);
                await _Top100NhacCachMang.SetAllInformation();
                _IsSetTop100NhacCachMang = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100Pop()
        {
            if (_IsSetTop100Pop)
                return;
            try
            {
                if (_LinkTop100Pop == "")
                    await GetLink();
                _Top100Pop = new ZingTop100Object(_LinkTop100Pop);
                await _Top100Pop.SetAllInformation();
                _IsSetTop100Pop = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100Rock()
        {
            if (_IsSetTop100Rock)
                return;
            try
            {
                if (_LinkTop100Rock == "")
                    await GetLink();
                _Top100Rock = new ZingTop100Object(_LinkTop100Rock);
                await _Top100Rock.SetAllInformation();
                _IsSetTop100Rock = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public async Task SetTop100Rap()
        {
            if (_IsSetTop100Rap)
                return;
            try
            {
                if (_LinkTop100Rap == "")
                    await GetLink();
                _Top100Rap = new ZingTop100Object(_LinkTop100Rap);
                await _Top100Rap.SetAllInformation();
                _IsSetTop100Rap = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        public async Task SetTop100Country()
        {
            if (_IsSetTop100Country)
                return;
            try
            {
                if (_LinkTop100Country == "")
                    await GetLink();
                _Top100Country = new ZingTop100Object(_LinkTop100Country);
                await _Top100Country.SetAllInformation();
                _IsSetTop100Country = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }


    }

}
